# encoding=utf-8

from pymongo import MongoClient


def init_mongo():
    host = '121.41.130.239'
    client = MongoClient(host, 27107)
    return client


def connect(client):
    # 连接mydb数据库,账号密码认证
    db = client.check56  # mydb数据库，同上解释
    db.authenticate("check56", "check56!!2021")
    collection = db.check56  # myset集合，同上解释
    print('数据库已连接')
    return collection


def exitMongo(client):
    print('关闭数据库连接')
    client.close()


def insert_mongo(collection, result):
    print('插入数据 ', result)
    collection.insert_one(result)

def insert_or_update(collection,result):
    status = collection.update(
        {"search_key": result['search_key']},
        {"$set": result},
        True
    )
    print(status)
